package com.cicadasmall.system.manager;

import com.cicadasmall.common.constant.Constant;
import com.cicadasmall.common.func.Fn;
import com.cicadasmall.common.utils.SpringContextUtils;
import com.cicadasmall.data.domain.DictDO;
import com.cicadasmall.system.service.IDictService;
import com.cicadasmall.system.vo.DictVO;
import com.cicadasmall.system.wrapper.DictWrapper;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

public class DictManager {

    private final static IDictService dictService = SpringContextUtils.getBean(IDictService.class);

    public static Optional<List<DictDO>> getOptionalByDictCode(String code) {
        List<DictDO> dictList = getByDictCode(code);
        if (Fn.isNotEmpty(dictList)) {
            return Optional.of(dictList);
        }
        return Optional.empty();
    }

    public static List<DictDO> getByDictCode(String code) {
        List<DictDO> dictList = dictService.findByCodeAndParentId(code, Constant.PARENT_ID);
        if (!Fn.isNotEmpty(dictList)) {
            return null;
        }
        return dictService.findByParentId(dictList.stream().findFirst().get().getId());
    }

    public static DictVO getByCodeAndValue(String code, String value) {
        DictDO sysDict = dictService.findByCodeAndValue(code, value);
        if (sysDict == null) {
            return null;
        }
        return DictWrapper.newBuilder().entityVO(sysDict);
    }

    public static DictVO getByDictId(Serializable id) {
        DictDO sysDict = dictService.getById(id);
        if (sysDict == null) {
            return null;
        }
        return DictWrapper.newBuilder().entityVO(sysDict);
    }
}